<?php
namespace app\api\controller;

use app\BaseController;
use app\model\ShopStore as SelfModel;
use app\model\Shop;
use app\model\ShopStoreType;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cookie;
use think\Response;
use think\Request;
class ShopStore extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\ShopStore";
        $this->list_rows = 10;
        $this->index = ['id','shop_id','shop_title','shop_store_type','shop_store_type_title','title','single_pic','people','price','deposit','sort','status','create_time'];
        $this->all = ['id','shop_id','shop_title','shop_store_type','shop_store_type_title','title','single_pic','people','price','deposit','sort','status','create_time'];
        $this->my = ['id','shop_id','shop_title','shop_store_type','shop_store_type_title','title','single_pic','people','price','deposit','sort','status','create_time'];
        $this->view = ['id','shop_id','shop_title','shop_store_type','shop_store_type_title','title','single_pic','people','price','deposit','sort','status','create_time'];
        $this->msg = '包厢';
        $this->order = ['sort'=>'asc','id'=>'desc'];
        $this->status = [['status','=','1']];
    }

    public function index(Request $request)
    {
        $data = SelfModel::withSearch(array_keys($request->param), $request->param)
        ->where([['status', '=', 1]])
        ->order(['sort' => 'asc', 'id' => 'desc'])
        ->paginate(20);
        return json(['code' => 200, 'message' => '列表', 'data' => $data]);
    }

    public function all(Request $request)
    {


        $data = SelfModel::withSearch(array_keys($request->param), $request->param)
            ->where([['status', '=', 1]])
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();

        return json(['code' => 200, 'message' => '全部(无分页)', 'data' => $data]);
    }



    public function create(Request $request)
    {
        $ret = SelfModel::create($request->param);
        if ($ret) {
        return json(['code' => 200, 'message' => '信息发布成功']);
        }
        return json(['code' => 400, 'message' => '详情']);
    }

    public function update(Request $request)
    {
        $info = SelfModel::find($request->id);
        $ret = $info->save($this->param);
        if ($ret !== false) {
        return json(['code' => 200, 'message' => '修改成功']);
        }
        return json(['code' => 200, 'message' => '失败']);
    }


    public function destory(Request $request)
    {
        $ret = SelfModel::where('id','in',$request->id)->update(['delete_time' => date('Y-m-d H:i:s')]);
        if ($ret) return json(['code' => 200, 'message' => '删除成功']);
        return json(['code' => 400, 'message' => '失败']);
    }

    public function view(Request $request)
    {
        $data = SelfModel::find($request->id);
        $data['content'] = str_replace('<img','<img style="width: 100%;!important;display:block;max-width:100%;text-align:center"',$data['content']);
        return json(['code' => 200, 'message' => '详情', 'data' => $data]);
    }

    public function html(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $content = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';
        $content .= $data;
        return Response::create($content, 'html');
    }

    public function json(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $data = str_replace('src="', 'src="' . $request->domain(), $data);
        return json($data);
    }

    public function my(Request $request)
    {
        $data = SelfModel::where('member_id', $request->member_id)->paginate(20);
        return json(['code' => 200, 'message' => '发布列表', 'data' => $data]);
    }




}
